package com.ruoyi.apiserver.services;

import com.ruoyi.apiserver.config.RabbitMQConfig;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class RabbitMQConsumerService {

    private final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 监听并处理简单字符串消息
     * @param message 消息内容
     */
    @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME)
    public void receiveSimpleMessage(String message) {
        System.out.println("接收到简单字符串消息: " + message);
        // 在这里添加处理逻辑
    }

    /**
     * 监听并处理Map类型消息
     * @param message 消息内容
     */
    @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME)
    public void receiveMessageWithMap(Map<String, Object> message) {
        System.out.println("接收到Map类型消息: " + message);
        // 在这里添加处理逻辑
        // 例如，可以将Map转换为JSON字符串输出
        try {
            String json = objectMapper.writeValueAsString(message);
            System.out.println("Map转JSON: " + json);
        } catch (JsonProcessingException e) {
            System.err.println("Map转JSON失败: " + e.getMessage());
        }
    }

    /**
     * 监听并处理通用对象消息
     * @param message 消息内容
     */
    @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME)
    public void receiveObjectMessage(Object message) {
        System.out.println("接收到对象消息: " + message);
        System.out.println("消息类型: " + message.getClass().getName());
        // 在这里添加处理逻辑
    }
}